package tr.edu.yildiz.ce.app_manager.ui.components.table;

import tr.edu.yildiz.ce.app_manager.service.BasicService;
import tr.edu.yildiz.ce.app_manager.ui.AppManagerApplication;
import tr.edu.yildiz.ce.app_manager.ui.action.menu.MenuAction;
import tr.edu.yildiz.ce.app_manager.ui.action.menu.MenuActionFactory;

import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.Button.ClickEvent;

public abstract class BasicTableForm<M> extends Form{

	Button newButton = new Button("New");
	Button removeButton = new Button("Remove");
	
	private Table table;
	protected M model;
	protected AppManagerApplication app;
	MenuActionFactory menuActionFactory = new MenuActionFactory();
	
	public BasicTableForm(AppManagerApplication app) {
		this.app = app;
		model = getModel();
		table = getTable();
		initTableStyle();
		addField("", table);
		initButtons();
	}
	
	private void initTableStyle(){
		table.setSizeFull();
		table.setHeight("160");
	}
	
	public abstract M getModel();
	public abstract Table getTable();
	protected Object selectedObject;
	
	private void initButtons() {
		newButton.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				MenuAction menuAction = menuActionFactory.getTableAction(model.getClass(), app);
				menuAction.doAction();
			}
		});

		removeButton.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				BasicService basicService = app.getServiceProvider().getService(model.getClass());
				basicService.remove(selectedObject);
			}
		});

		HorizontalLayout footer = new HorizontalLayout();
		footer.setSpacing(true);
		footer.addComponent(newButton);
		footer.addComponent(removeButton);
		setFooter(footer);
//		addField("", newButton);
//		addField("", removeButton);

	}
	
	
	
	
	
	
}
